$(function(){
	e();
});

function le_error_message(le_position,message){
    if($("div").hasClass("le-error-message")){
        $(le_position).text(message).fadeIn();
        $(le_position).delay(2000).fadeOut();
    }
}

var myScroll,slider,codeFlag = true;
function init(){
    myScroll = new IScroll('.alert-cont',{
        mouseWheel: true,
        scrollbars: true
    });
}
// 如果点击（清除手机号）按钮，则轻清空输入
$(".le-number .le-clean-phone").click(function(){
    $(".input-phone").val("");
});
// 如果点击（验证码）按钮，则轻清空输入
$(".le-code .le-clean-pass").click(function(){
    $(".input-code").val("");
});
// 如果点击（密码）按钮，则轻清空输入
$(".le-password .le-clean-pass").click(function(){
    $(".input-repassword").val("");
});
// 验证是否正确填写电话
$( ".input-phone" ).focusout(function(){
    var mobile = $(".input-phone").val();
    if(!mobile){
        le_error_message(".le-number>.le-error-message","手机号码不能为空");
        return false;
    }
    if(!(/^1[34578]\d{9}$/.test(mobile))){
        le_error_message(".le-number>.le-error-message","手机号码格式有误，请重新输入");
        return false;
    }
});
$( ".input-repassword" ).focusout(function(){
    var password = $(".input-repassword").val();
    if(!password){
        le_error_message(".le-password>.le-error-message","密码不能为空");
        return false;
    }
});
var flags = false;
function e(){
    // 信息弹窗
    $(".btn_rule").on("click",function(){
        $(".alert-box").fadeIn();
        myScroll.refresh();
    });
    // 关闭弹窗
    $(".alert-close").on("click",function(){
        $(".alert-box").fadeOut();
    });
	// 注册提交
	$(".form-btn1").on("click",function(){
		var ip = $('.part2 .input-phone').val();
		var regBox = {
			regMobile: /^0?1[3|4|5|7|8][0-9]\d{8}$/, //手机
			//			regTel: /^0[\d]{2,3}-[\d]{7,8}$/
		};
		var mflag = regBox.regMobile.test(ip);
		//		var tflag = regBox.regTel.test(ip);
		if(!mflag) {
            le_error_message(".le-number >.le-error-message","手机号格式有误，请重新输入");
			// 手机号码错误
			phoneError();
			return false;
		}
		// window.location.href = "mine.html";
	});
	$(".part2 .input-phone").focus(function(){
		// 取消手机号码错误提示
		 canclePhoneError();
	});
	// 点击获取验证码
	$(".btn-code").mousedown(function(){
	    if(!flags){
            slideBtn();
        }else{
            btnCodeTimeOut();
        }
	});
}

//预设（获取验证码）无法点击
//$(".le-forget-form .input-code").attr("disabled",true);

//滑动事件
function slideBtn() {
    var ip = $('.part2 .input-phone').val();
    var regBox = {
        regMobile: /^0?1[3|4|5|7|8][0-9]\d{8}$/, //手机
        //			regTel: /^0[\d]{2,3}-[\d]{7,8}$/
    };
    var mflag = regBox.regMobile.test(ip);
    if(mflag) {
        if(codeFlag){
            $(".slide-box").fadeIn();

            var $container = $('#foo');
            $container.slideToUnlock({
                width: 360,
                height: 60,
                bgColor:'#FEEC97',
                handleColor:'#E67E22',
                succColor:'#E5EE9F',
                text:'',
                textColor:'#000',
                succText:'解锁成功',
                successFunc: function() {
                    // alert('successfully unlock!');
                    // 解锁成功
                    setTimeout(function(){
                        //开始验证
                        $(".slide-box").fadeOut();
                        btnCodeTimeOut();
                        btnCodeAc();
                        flags = true;
                        $(".le-forget-form .input-code").attr("disabled",false);                  //开始验证后才可输入验证码
                        $(".le-clean-code").click(function(){ $(".input-code").val(""); });        //可以清除验证码
                    },300);
                }
            });
            codeFlag = false;
        }

    }else{
        le_error_message(".le-forget-form .le-number >.le-error-message","手机号格式有误，请重新输入");
        return false;
    }
}

// 手机号码错误
function phoneError(){
	$(".input-phone").addClass("error").attr('placeholder','请输入正确手机号');
}
function canclePhoneError(){
	$(".input-phone").removeClass("error").attr('placeholder','手机号')
}
// 防作弊的图形验证码检验正确
function btnCodeAc(){
	$(".btn-code").addClass("on");
}
function btnCodeCancleAc(){
	$(".btn-code").removeClass("on");
}
// 验证码60秒倒计时
var codeTime = 60;
function btnCodeTimeOut(){
	$(".btn-code").addClass("off").html(codeTime+"s");
    sms_send();
	var timer = setInterval(function(){
		codeTime--;
		$(".btn-code").html(codeTime+"s");
		if(codeTime < 1){
			clearInterval(timer);
			$(".btn-code").html("获取验证码");
			$(".btn-code").removeClass("off");
			codeTime = 60;
		}
	},1000);
}

//发送短信验证码
function sms_send(){
    var mobile = $('.part2 .input-phone').val();
    var regBox = {
        regMobile: /^0?1[3|4|5|7|8][0-9]\d{8}$/, //手机
        //			regTel: /^0[\d]{2,3}-[\d]{7,8}$/
    };
    var mflag = regBox.regMobile.test(mobile);
    var post = {"phone":mobile,"is_type":2};
    if(mflag){
        $.ajax({
            type: "POST",dataType:"json", url: "index.php?s=user&c=async&m=sms_send", data: post,
            success: function(data) {
                if (data.status) {
                    le_error_message(".le-forget-form .le-code >.le-error-message","手机号格式有误，请重新输入");
                    //dr_tips('发送验证码成功....', 3, 1);
                    // setTimeout('window.location.href="'+data.backurl+'"', 2000);
                } else {
                    // $('#dr_code_img').click();
                    le_error_message(".le-forget-form .le-code >.le-error-message",data.msg);
                    //dr_tips(data.msg,3,1);
                }
            },
            error: function(HttpRequest, ajaxOptions, thrownError) {
                alert(HttpRequest.responseText);
            }
        });

    }else{
        le_error_message(".le-forget-form .le-number >.le-error-message","手机号格式有误，请重新输入");
        return false;
    }
}
//验证表单，是否正确填写验证码
$( ".input-code" ).focusout(function(){
    var mobile = $(".input-phone").val(),
        code = $(".input-code").val();

    if(!mobile){
        return false;
    }
    if(!(/^1[34578]\d{9}$/.test(mobile))){
        return false;
    }
    if(!code){
        le_error_message(".le-code>.le-error-message","验证码不能为空");
        return false;
    }

    //code_type =  1 因为在资料库的资料表当中，1为「注册」类型

    var post = {'mobile':mobile,'code':code,'code_type':1};
    $.ajax({

        //url: "index.php?s=user&c=register&m=check_mobile_code" , 连结到指定档案中的该class
        //check_mobile_code为class名称，register为php名称

        type: "POST",dataType:"json", url: "index.php?s=user&c=register&m=check_mobile_code", data: post,
        success: function(data) {

            //用此条方法可验证，资料有无取得
            //console.log("data: "+data);return false;
            // 目前进行到此步骤时，data为null
            //console.log("data.status: "+data.status);return false;

            if (data.status) {
                le_error_message(".le-code>.le-error-message","验证码输入正确");
            } else {
                $('#dr_code_img').click();
                le_error_message(".le-code>.le-error-message","验证码输入错误，请重新输入");
                dr_tips(data.code,3,1);
            }
        },
        error: function(HttpRequest, ajaxOptions, thrownError) {
            alert(HttpRequest.responseText);
        }
    });
});
// 是否勾选用户协议
var isRead = true;
$(".form-btn3").on("click",function(){
	if(isRead){
		$(".form-btn3").removeClass("on");
		isRead = false;
	}else{
		$(".form-btn3").addClass("on");
		isRead = true;
	}
});
$(".form-btn3 a").on("click",function(e){
	window.event? window.event.cancelBubble = true : e.stopPropagation();
	$(".form-btn3").addClass("on");
	isRead = true;
});


$(".le_forget .forget_submit").on("click",function(){
    var phone = $(".input-phone").val(),
        code = $(".input-code").val(),
        pass = $(".input-repassword").val();
    if(!phone){
        le_error_message(".le-forget-form .le-number .le-error-message","手机号码不能为空");
        return false;
    }
    if(!(/^1[34578]\d{9}$/.test(phone))){
        le_error_message(".le-forget-form .le-number .le-error-message","手机号码有误");
        return false;
    }
    if(!code){
        le_error_message(".le-forget-form .le-code >.le-error-message","验证码不为空");
        return false;
    }
    if(!pass){
        le_error_message(".le-forget-form .le-password>.le-error-message","新密码不能为空");
        return false;
    }

    var post = {"phone":phone,"code":code,"pass":pass};
    $.ajax({
        type:"POST",dataType:"json",url:"",data:post,
        success:function (data) {console.log(data);
            if(data.status){
                // dr_tips('找回密码成功，即将为您跳转....', 3, 1);
                $(".le-forget-form .forget_submit").text("找回密码成功，即将为您跳转....").css("background","rgb(189, 189, 189)").css("font-size","16px");
                //dr_tips(data.msg,3,1);
                setTimeout('window.location.href="'+data.id+'"',2000);
            }else{
                le_error_message(".le-password>.le-error-message",data.msg);
                //dr_tips(data.msg,3,1);
            }
        },
        error: function(HttpRequest, ajaxOptions, thrownError) {
            alert(HttpRequest.responseText);
        }
    });
});

$(".le_poup_forget .forget_submit").on("click",function(){
    var phone = $(".input-phone").val(),
        code = $(".input-code").val(),
        pass = $(".input-repassword").val();
    if(!phone){
        le_error_message(".le-forget-form .le-number .le-error-message","手机号码不能为空");
        return false;
    }
    if(!(/^1[34578]\d{9}$/.test(phone))){
        le_error_message(".le-forget-form .le-number .le-error-message","手机号码有误");
        return false;
    }
    if(!code){
        le_error_message(".le-forget-form .le-code >.le-error-message","验证码不为空");
        return false;
    }
    if(!pass){
        le_error_message(".le-forget-form .le-password>.le-error-message","新密码不能为空");
        return false;
    }

    var post = {"phone":phone,"code":code,"pass":pass};
    $.ajax({
        type:"POST",dataType:"json",url:"",data:post,
        success:function (data) {console.log(data);
            if(data.status){
                // dr_tips('找回密码成功，即将为您跳转....', 3, 1);
                $(".le-forget-form .forget_submit").text("找回密码成功，即将为您跳转....").css("background","rgb(189, 189, 189)").css("font-size","16px");
                //dr_tips(data.msg,3,1);
                setTimeout('window.location.href="./index.php?s=user&c=login&m=poup_login"',2000);
            }else{
                le_error_message(".le-password>.le-error-message",data.msg);
                //dr_tips(data.msg,3,1);
            }
        },
        error: function(HttpRequest, ajaxOptions, thrownError) {
            alert(HttpRequest.responseText);
        }
    });
});


//失去焦点查询号码是否存在
$("#register_phone").blur(function(){
    var phone = $(this).val();
    if(!phone){
        le_error_message(".le-number>.le-error-message","手机号码不能为空");
        return false;
    }
    if(!(/^1[34578]\d{9}$/.test(phone))){
        le_error_message(".le-number>.le-error-message","手机号码格式有误，请重新输入");
        return false;
    }
    var post = {'phone':phone};
    $.ajax({
        type:"POST",dataType:"json",url:'index.php?s=user&c=register&m=check_phone',data:post,
        success:function (data) {
            if(data.status){
                // $(".input-phone").addClass("error").attr('placeholder','该手机号码已经注册请重新输入');
                //dr_tips('该手机号码已经注册，请重新输入', 3, 1);
                $(".btn-send-code").mousedown(function(){
                    if(!flags){
                        slideBtn();
                    }else{
                        btnCodeTimeOut();
                    }
                });
            }else{
                $(".input-phone").addClass("error").attr('placeholder','用户不存在');
                dr_tips(data.msg,3);
            }
        },
        error: function(HttpRequest, ajaxOptions, thrownError) {
            alert(HttpRequest.responseText);
        }
    });
});